package com.sam;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mysql.jdbc.Driver;

public class Product {
        int id;
        String name;
        String desction;
        int price;
        int stock;
        String pic;
        String remark;
        ArrayList<Product> products = new ArrayList<Product>();

        public Product() {

        }

        protected Product(int id, String name, String desction, int price,
				int stock, String pic, String remark) {
			super();
			this.id = id;
			this.name = name;
			this.desction = desction;
			this.price = price;
			this.stock = stock;
			this.pic = pic;
			this.remark = remark;
		}


		public int getId() {
                return id;
        }

        public void setId(int id) {
                System.out.println("setId called:" + id);
                if (products.size() == 0)
                        products = getProducts();
                for (Product p : products) {
                        if (p.id == id) {
                                this.name = p.name;
                                this.desction = p.desction;
                                this.price = p.price;
                                this.stock = p.stock;
                                this.pic = p.pic;
                                this.remark = p.remark;
                                break;
                        }
                }
                this.id = id;
        }

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getDesction() {
			return desction;
		}

		public void setDesction(String desction) {
			this.desction = desction;
		}

		public int getPrice() {
			return price;
		}

		public void setPrice(int price) {
			this.price = price;
		}

		public int getStock() {
			return stock;
		}

		public void setStock(int stock) {
			this.stock = stock;
		}

		public String getPic() {
			return pic;
		}

		public void setPic(String pic) {
			this.pic = pic;
		}

		public String getRemark() {
			return remark;
		}

		public void setRemark(String remark) {
			this.remark = remark;
		}

		public ArrayList<Product> getProducts() {
                products.clear();
                Connection conn = null;
                PreparedStatement pstmt = null;
                try {
                        DriverManager.registerDriver(new Driver());
                        conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8", "j102u17", "j102u17.989");
                        String sql = "select * from products";
                        pstmt = conn.prepareStatement(sql);
                        ResultSet rs = pstmt.executeQuery();
                        while (rs.next()) {
                                int id = rs.getInt("id");
                                String name = rs.getString("name");
                                String desction = rs.getString("desction");
                                int price = rs.getInt("price");
                                int stock = rs.getInt("stock");
                                String pic = rs.getString("pic");
                                Product p = new Product(id, name, desction, price, stock, pic, remark);
                                products.add(p);
                        }
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } finally {
                        try {
                                if (conn != null && pstmt != null) {
                                        pstmt.close();
                                        conn.close();
                                }
                        } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                return products;
        }

        public void setProducts(ArrayList<Product> products) {
                this.products = products;
        }

}